local love = require 'love'
local fontHandler = require('assets-handler.font-handler')

local setColor = love.graphics.setColor
local drawRectangle = love.graphics.rectangle
local printg = love.graphics.print
local setLineWidth = love.graphics.setLineWidth

local width = Width
local height = Height
local padding = Padding

local healthBar = {
    x = 0,
    y = 0,
    width = 0,
    height = 0,
    health = 100,
    maxHealth = 100,
    healthPercentage = 0,
    healthWidth = 0,
    text = "",
    setText = "",
    textX = 0,
    textY = 0,
    textWidth = 0,
    textHeight = 0,
    timer = 0,
    duration = 1.5,
    fontSize = 15,
    state = 0 -- 1：开始；2：进行中；3：结束
}

local function init()
    local instructionsRadius = 10 -- instructions 按钮半径
    local restartWidth = 25 -- restartButton 宽度

    healthBar.x = instructionsRadius * 2 + padding * 2
    healthBar.y = padding
    healthBar.width = width - padding * 4 - instructionsRadius * 2 - restartWidth
    healthBar.height = height / 30

    -- 初始化字体
    fontHandler.setFont(fontHandler.fontSize)
    healthBar.textHeight = fontHandler.getFontHeight()
    healthBar.textY = healthBar.y + healthBar.height/ 2 - healthBar.textHeight/ 2.4
end

local function setHealth(currentHealth)
    healthBar.health = currentHealth
end

local function setText(healthBarText)
    healthBar.setText = healthBarText
    healthBar.timer = 0
end

local function update(dt)
    -- 动态显示文本
    healthBar.timer = healthBar.timer + dt
end

-- 绘制生命条（绿黄红渐变）
local function drawHealthBar(x, y, w, h, currentHealth, currentMaxHealth)
    -- 计算生命条宽度和颜色
    healthBar.healthPercentage = currentHealth / currentMaxHealth
    healthBar.healthWidth = w * healthBar.healthPercentage

    -- 根据生命值百分比确定颜色
    local r, g, b
    if healthBar.healthPercentage > 0.5 then
        r = 1 - 2 * (1 - healthBar.healthPercentage)  -- 从0到1
        g = 1  -- 保持绿色
    elseif healthBar.healthPercentage > 0.25 then
        r = 1  -- 保持红色
        g = 2 * healthBar.healthPercentage  -- 从0到1
    else
        r = 1  -- 保持红色
        g = 0  -- 低生命值时无绿色
    end
    b = 0  -- 不使用蓝色

    -- 绘制生命条（渐变颜色）
    setColor(r, g, b)
    drawRectangle("fill", x, y, healthBar.healthWidth, h)

    -- 绘制边框
    setColor(0, 0, 0)
    drawRectangle("line", x, y, w, h)

    -- 显示开始与结束提示词
    if currentHealth == currentMaxHealth then
        healthBar.state = 1
        healthBar.text = "请出牌, 对手生命值 " .. currentHealth
    elseif currentHealth <= 0 then
        healthBar.state = 3
        healthBar.text = "你获得了胜利"
    else
       healthBar.state = 2
       -- 显示当前生命值和百分比
        if healthBar.timer < healthBar.duration then
            healthBar.text = healthBar.setText
        else
            healthBar.text =  string.format("HP: %.0f / %.0f (%.0f%%)", currentHealth, currentMaxHealth, healthBar.healthPercentage * 100)
        end
    end

    fontHandler.setFont(healthBar.fontSize)
    healthBar.textWidth= fontHandler.getFontWidth(healthBar.text)
    healthBar.textX = healthBar.x + healthBar.width/ 2 - healthBar.textWidth/ 2

    setColor(0, 0, 0, 0.9)
    printg(healthBar.text, healthBar.textX, healthBar.textY)
end

local function drawBar()
    -- 生命条背景
    setColor(1, 1, 1)
    drawRectangle("fill", healthBar.x, healthBar.y, healthBar.width, healthBar.height)

    -- 生命条
    drawHealthBar(healthBar.x, healthBar.y, healthBar.width, healthBar.height, healthBar.health, healthBar.maxHealth)

    -- 绘制边框
    setColor(1, 1, 1, 0.8)
    setLineWidth(1)
    drawRectangle("line", healthBar.x, healthBar.y, healthBar.width, healthBar.height)
end

return{
    init = init,
    drawBar = drawBar,
    setHealth = setHealth,
    setText = setText,
    update = update,
    setMaxHealth = function(currentMaxHealth) healthBar.maxHealth = currentMaxHealth end
}